package main

import (
	"context"
	"fmt"
	"net"
	hello_grpc "send_file/grpc_proto/hello_grpc"

	"google.golang.org/grpc"
	"google.golang.org/grpc/grpclog"
)

type HelloService struct {
	hello_grpc.UnimplementedHelloServiceServer
}

func (h HelloService) SayHello(ctx context.Context, request *hello_grpc.HelloRequest) (*hello_grpc.HelloResponse, error) {
	fmt.Println(request)
	return &hello_grpc.HelloResponse{
		Name:    "lym",
		Message: "ok",
	}, nil
}

func main() {
	// 监听端口
	listen, err := net.Listen("tcp", ":8080")
	if err != nil {
		grpclog.Fatalf("Failed to listen err:%v", err)
	}

	// 创建一个grpc服务器实例
	s := grpc.NewServer()
	server := HelloService{}

	// 将server结构体注册为grpc服务
	hello_grpc.RegisterHelloServiceServer(s, &server)
	fmt.Println("grpc server running:8080")

	// 开始处理客户端请求
	err = s.Serve(listen)
}
